﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Kaidou.Model;
using Kaidou.Persister;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using Kaidou.Util;
using Kaidou.Bll;
using System.Text.RegularExpressions;

public partial class seal_seal_order_search : System.Web.UI.Page
{
    private int pageSize = int.Parse(Resources.Resource.pagenumber);

    protected void Page_Load(object sender, EventArgs e)
    {
        LoginModel loginModel = (LoginModel)Session["loginModel"];

        if (loginModel == null)
        {
            Response.Redirect("../login.aspx");
        }
        else if ( loginModel.isKumiaiin())
        {
            Response.Redirect("../menu.aspx");
        }

        if (loginModel.isKumiaiin() && sessioncontrol.control())
        {
            Response.Redirect("../control.aspx");
        }


        try
        {

             if (!IsPostBack)
             {
                   JUCHU_STR_DATE.Text = System.DateTime.Now.ToString("yyyyMMdd");
                   JUCHU_END_DATE.Text = System.DateTime.Now.ToString("yyyyMMdd");

                   table.Visible = false;
                   none.Visible = false;
            }
            

            if (Session["back"] != null && (int.Parse(Session["back"].ToString()) == 1))
            {

                OrderSearchModel M = (OrderSearchModel)Session["searchOrderModel"];
                if (M.getJuchu_str_date() != null)
                {
                    JUCHU_STR_DATE.Text = M.getJuchu_str_date();
                }
                else
                {
                    JUCHU_STR_DATE.Text = "";
                }

                if (M.getJuchu_end_date() != null)
                {
                    JUCHU_END_DATE.Text = M.getJuchu_end_date();
                }
                else
                {
                    JUCHU_END_DATE.Text = "";
                }

                if (M.getUnion_code()!=null)
                {
                    UNION_CODE.Text = M.getUnion_code();
                }

                if (M.getOffice_name()!=null)
                {
                    OFFICE_NAME.Text = M.getOffice_name();
                }

                if (M.getOffice_kana()!=null)
                {
                    OFFICE_KANA.Text = M.getOffice_kana();
                }

                if(M.getBranch_name()!=null){
                    BRANCH_NAME.Text=M.getBranch_name();
                }

                string trnjusts = M.getJuchu_sts();
                if (trnjusts != null)
                {


                    if (trnjusts.Equals("10"))
                    {
                        TRN_JUCHU_STS2.Checked = true;
                    }

                    else if (trnjusts.Equals("20"))
                    {
                        TRN_JUCHU_STS3.Checked = true;
                    }

                    else if (trnjusts.Equals("30"))
                    {
                        TRN_JUCHU_STS4.Checked = true;
                    }

                    else
                    {
                        TRN_JUCHU_STS1.Checked = true;
                    }

                }

                
                string trnclass = M.getJuchu_clss();
                if (trnclass != null)
                {
                    if (trnclass.Equals("20"))
                    {
                        TRN_JUCHU_CLSS2.Checked = true;
                    }
                    if (trnclass.Equals("10"))
                    {
                        TRN_JUCHU_CLSS3.Checked = true;
                    }
                }
                string faxerrorflag = M.getFax_error_flg();

                if (faxerrorflag!=null && faxerrorflag.Equals("1"))
                {
                    FAX_ERROR_FLG.Checked = true;
                }
               

                float str_rate = M.getReport_str_rate();
                if (str_rate > -1)
                {
                    REPORT_STR_RATE.Text = str_rate.ToString();
                }

                float end_rate = M.getReport_end_rate();
                if (end_rate > -1)
                {
                    REPORT_END_RATE.Text = end_rate.ToString();
                }

                /*
                if (M.getOcr_order_qty() == 1)
                {
                    OCR_ORDER_QTY.Checked = true;
                }
                 * */
               
              
                if (M.getJuchu_no() != null)
                {
                    TRN_JUCHU_NO.Text = M.getJuchu_no();
                }
             


                page(int.Parse(Session["pageNo"].ToString()), true);

                Session.Remove("back");
            }

        }catch( Exception mee){
              LogManager.error("seal_seal_order_search fail "+mee.Message+mee.StackTrace);
        }

    }


    private void page(int no, bool back)
    {
        LoginModel loginModel = (LoginModel)Session["loginModel"];
        //カプセル化オブジェクト
        OrderSearchModel M = new OrderSearchModel();
        if (!back)
        {
            M.setJuchu_str_date(JUCHU_STR_DATE.Text.Trim().Length == 8 ? JUCHU_STR_DATE.Text.Trim() : null);
            M.setJuchu_end_date(JUCHU_END_DATE.Text.Trim().Length == 8 ? JUCHU_END_DATE.Text.Trim() : null);
            M.setUnion_code(UNION_CODE.Text.Trim().Length == 0 ? null : UNION_CODE.Text.Trim());
            M.setOffice_name(OFFICE_NAME.Text.Trim().Length == 0 ? null : OFFICE_NAME.Text.Trim());
            M.setOffice_kana(OFFICE_KANA.Text.Trim().Length == 0 ? null : OFFICE_KANA.Text.Trim());
            M.setBranch_name(BRANCH_NAME.Text.Trim().Length == 0 ? null : BRANCH_NAME.Text.Trim());
            M.setBranch_code(-1);
            string trnjusts=null;
            if (TRN_JUCHU_STS2.Checked)
            {
                trnjusts = "10";
            }

            if (TRN_JUCHU_STS3.Checked)
            {
                trnjusts = "20";
            }

            
            if (TRN_JUCHU_STS4.Checked)
            {
                trnjusts = "30";
            }
            M.setJuchu_sts(trnjusts);

            
            string trnclass=null;
            if (TRN_JUCHU_CLSS2.Checked)
            {
                trnclass = "20";
            }
            if (TRN_JUCHU_CLSS3.Checked)
            {
                trnclass = "10";
            }
            M.setJuchu_clss(trnclass);
           
            string faxerrorflag=null;
            if (FAX_ERROR_FLG.Checked)
            {
                faxerrorflag = "1";
            }
            M.setFax_error_flg(faxerrorflag);

            if (REPORT_STR_RATE.Text.Trim().Length == 0)
            {
                M.setReport_str_rate(-1);
            }

            else
            {
                try
                {
                    M.setReport_str_rate(float.Parse(REPORT_STR_RATE.Text));
                }
                catch (Exception pe)
                {
                    M.setReport_str_rate(-1);
                }
              
            }

            if (REPORT_END_RATE.Text.Trim().Length == 0)
            {
                M.setReport_end_rate(-1);
            }
            else
            {
                try
                {
                    M.setReport_end_rate(float.Parse(REPORT_END_RATE.Text));
                }
                catch(Exception pe)
                {
                    M.setReport_end_rate(-1);
                }

            }

            /*
            if (OCR_ORDER_QTY.Checked)
            {
                M.setOcr_order_qty(1);
            }*/

            M.setJuchu_no(TRN_JUCHU_NO.Text.Trim().Length == 0 ? null : TRN_JUCHU_NO.Text.Trim());

            //------------------------------------------validation--------------------------------------
            InputErrorException ie = new InputErrorException(Resources.Resource.ResourceManager);
            if (!StringUtil.isEmpty(JUCHU_STR_DATE.Text))
            {
                if (!StringUtil.isNumber(JUCHU_STR_DATE.Text))
                {
                    ie.addErrorCode("E9001");
                }

                if (JUCHU_STR_DATE.Text.Length < 8)
                {
                    ie.addErrorCode("E9001");
                }
            }

            if (!StringUtil.isEmpty(JUCHU_END_DATE.Text))
            {
                if (!StringUtil.isNumber(JUCHU_END_DATE.Text))
                {
                    ie.addErrorCode("E9001");
                }

                if (JUCHU_END_DATE.Text.Length < 8)
                {

                    ie.addErrorCode("E9001");
                }
            }

            if (!StringUtil.isEmpty(REPORT_STR_RATE.Text))
            {
                if (!StringUtil.isNumber(REPORT_STR_RATE.Text))
                {
                    ie.addErrorCode("E9002");
                }
            }

            if (!StringUtil.isEmpty(REPORT_END_RATE.Text))
            {
                if (!StringUtil.isNumber(REPORT_END_RATE.Text))
                {
                    ie.addErrorCode("E9002");
                }
            }


            if (M.getJuchu_no() != null)
            {
                Regex r = new Regex(@"^[a-zA-Z]{1}\d{9}$");
                if (!r.IsMatch(M.getJuchu_no()))
                {
                    ie.addErrorCode("E9003");
                }

            }
            List<string> errorCodeList = ie.getErrorMessages();
            msg.Text = ErrorCodeUtil.errorCodeListToHtml(errorCodeList);

            if (errorCodeList.Count > 0)
            {
                table.Visible = false;
                none.Visible = true;
                return;
            }
            //------------------------------------------validation--------------------------------------
            

            //検索条件のチェック、エラーがあれば検索しない。
            if (!StringUtil.isEmpty(M.getJuchu_str_date()) && !StringUtil.isNumber(M.getJuchu_str_date()))
            {
                table.Visible = false;
                none.Visible = true;
                return;
            }
            if (!StringUtil.isEmpty(M.getJuchu_end_date()) && !StringUtil.isNumber(M.getJuchu_end_date()))
            {
                table.Visible = false;
                none.Visible = true;
                return ;
            }

            //組合員コードのチェック、エラーがあれば検索しない。
            if (!StringUtil.isEmpty(M.getUnion_code()) && !StringUtil.IsHankaku(M.getUnion_code()))
            {
                table.Visible = false;
                none.Visible = true;
                return;
            }

            //報告率のチェック、エラーがあれば検索しない。
            if (M.getReport_str_rate() < -1)
            {
                table.Visible = false;
                none.Visible = true;
                return ;
            }

        }
        else
        {
            M = (OrderSearchModel)Session["searchOrderModel"];

        }

        Session["searchOrderModel"] = M;
        Session["pageNo"] = no;

        TRN_Juchu_HPersister dao = new TRN_Juchu_HPersister();
        if (no <= 0) no = 1;
        Pager pager = dao.search(M, no, pageSize);
        //if (no > pager.Pages) no = pager.Pages;
        //pager = dao.search(M, no, pageSize);
        pageNo.Value = no.ToString();
        pages.Text = "全" + pager.Total + "件";
        ArrayList list = pager.List;

        if (list.Count == 0)
        {
            table.Visible = false;
            none.Visible = true;
        }
        else
        {
            none.Visible = false;
            table.Visible = true;
            //datatable.Rows.Clear();
            if (no == 1)
            {
                prebutton.Visible = false;
            }
            else
            {
                prebutton.Visible = true;
                prebutton.Text = "≪ 前の" + pageSize + "件";

            }

            if (no == pager.Pages)
            {
                nextbutton.Visible = false;

            }
            else
            {
                nextbutton.Visible = true;
                nextbutton.Text = " 次の" + pageSize + "件 ≫";
            }

            MST_Union_BranchPersister union_branchMgr = new MST_Union_BranchPersister();
            MST_UnionPersister unionMgr = new MST_UnionPersister();
            int standardrate = int.Parse(Resources.Resource.kaidou_forceorder_rate);
            StringBuilder sb = new StringBuilder();
            foreach (object o in list)
            {
                OrderSearchModel mu = (OrderSearchModel)o;

                MstUnion union = unionMgr.getDetail(mu.getUnion_code());

                mst_union_branch union_branch = union_branchMgr.getDetail(mu.getUnion_code(), mu.getBranch_code());

                bool errorflag = false;

                if (mu.isError())
                {
                    errorflag = true;
                }
                if (standardrate > mu.getReport_rate())
                {
                    errorflag = true;
                }
                //errorflag?Color.Red:Color.White
                TableRow row = new TableRow();

                TableCell td = new TableCell();
                td.BackColor = Color.White;
                td.Height = 30;
                if (errorflag) 
                { 
                    td.ForeColor=Color.Red;
                }
                td.HorizontalAlign = HorizontalAlign.Center;
                td.Text = mu.getJuchu_date().ToString("yyyy/MM/dd");
                row.Cells.Add(td);

                td = new TableCell();
                td.BackColor = Color.White;
                td.Height = 30;
                if (errorflag)
                {
                    td.ForeColor = Color.Red;
                }
                td.Text = union.OfficeName;
                row.Cells.Add(td);

                
                td = new TableCell();
                td.BackColor = Color.White;
                td.Height = 30;
                if (errorflag)
                {
                    td.ForeColor = Color.Red;
                }
                td.Text = union_branch == null ? "本店" : union_branch.officename;
                row.Cells.Add(td);

               
                td = new TableCell();
                td.BackColor = Color.White;
                td.Height = 30;
                if (errorflag)
                {
                    td.ForeColor = Color.Red;
                }
                td.HorizontalAlign = HorizontalAlign.Center;
                td.Text = mu.getJuchu_clss_moji();
                row.Cells.Add(td);
               

                td = new TableCell();
                td.BackColor = Color.White;
                td.Height = 30;
                if (errorflag)
                {
                    td.ForeColor = Color.Red;
                }
                td.HorizontalAlign = HorizontalAlign.Center;
                td.Text = mu.getJuchu_no();
                row.Cells.Add(td);

                td = new TableCell();
                td.BackColor = Color.White;
                td.Height = 30;
                if (errorflag)
                {
                    td.ForeColor = Color.Red;
                }
                td.HorizontalAlign = HorizontalAlign.Right;
                td.Text = mu.getTotal_seal_qty().ToString();
                row.Cells.Add(td);


                td = new TableCell();
                td.BackColor = Color.White;
                td.Height = 30;
                if (errorflag)
                {
                    td.ForeColor = Color.Red;
                }
                td.HorizontalAlign = HorizontalAlign.Left;
                td.Text = mu.getJuchu_sts_moji();
                row.Cells.Add(td);

                td = new TableCell();
                td.BackColor = Color.White;
                td.Height = 30;
                if (errorflag)
                {
                    td.ForeColor = Color.Red;
                }
                td.HorizontalAlign = HorizontalAlign.Left;
                td.Text = mu.getErrorFlg();
                row.Cells.Add(td);

                td = new TableCell();
                td.BackColor = Color.White;
                td.Height = 30;
                if (errorflag)
                {
                    td.ForeColor = Color.Red;
                }
                td.HorizontalAlign = HorizontalAlign.Left;
                td.Text = mu.getReport_rate().ToString();
                row.Cells.Add(td);

               
                td = new TableCell();
                td.BackColor = Color.White;
                td.Height = 30;
                if (errorflag)
                {
                    td.ForeColor = Color.Red;
                }
                td.HorizontalAlign = HorizontalAlign.Left;
                td.Text = mu.getOcrOrderFlg();
                row.Cells.Add(td);
                

                
                td = new TableCell();
                td.BackColor = Color.White;
                td.Height = 30;
                td.HorizontalAlign = HorizontalAlign.Center;

                if (loginModel.isJimukyoku())
                {
                    if (TrnJuchuh.JUCHU_STATUS_NOTORDERD.Equals(mu.getJuchu_sts()))
                    {
                        td.Text = "<a href='seal_order_detail.aspx?juchu_no=" + mu.getJuchu_no() + "'>表示</a>";
                    }
                    else if (TrnJuchuh.JUCHU_STATUS_ORDERD.Equals(mu.getJuchu_sts()) || TrnJuchuh.JUCHU_STATUS_DELIVERD.Equals(mu.getJuchu_sts()))
                    {
                        td.Text = "<a href='seal_ordered_detail.aspx?juchu_no=" + mu.getJuchu_no() + "'>表示</a>";
                    }
                }

                if (loginModel.isOperator())
                {
                    if (TrnJuchuh.JUCHU_STATUS_NOTORDERD.Equals(mu.getJuchu_sts()))
                    {
                        td.Text = "<a href='seal_order_detail.aspx?juchu_no=" + mu.getJuchu_no() + "'>表示</a>";
                    }
                    else if (TrnJuchuh.JUCHU_STATUS_ORDERD.Equals(mu.getJuchu_sts()) || TrnJuchuh.JUCHU_STATUS_DELIVERD.Equals(mu.getJuchu_sts()))
                    {
                        td.Text = "<a href='seal_order_paper.aspx?juchu_no=" + mu.getJuchu_no() + "&PAPERVIEW=off'>表示</a>";
                    }
                }
                
                row.Cells.Add(td);

                datatable.Rows.Add(row);
            }

        }
    }

    protected void pre(object sender, EventArgs e)
    {
        Session.Remove("back");
        Session.Remove("searchOrderModel");
        Session.Remove("pageNo");
        page(int.Parse(pageNo.Value) - 1, false);
    }

    protected void next(object sender, EventArgs e)
    {
        Session.Remove("back");
        Session.Remove("searchOrderModel");
        Session.Remove("pageNo");
        page(int.Parse(pageNo.Value) + 1, false);
    }

    protected void search(object sender, EventArgs e)
    {
        Session.Remove("back");
        Session.Remove("searchOrderModel");
        Session.Remove("pageNo");
        page(1, false);


    }
}
